import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-04-15
 * Time: 21:31
 */
//用队列实现栈
public class MyStack {
        public Queue<Integer> queue1;
        public Queue<Integer> queue2;

        public MyStack(){
            queue1 = new LinkedList<>();
            queue2 = new LinkedList<>();
        }

        public void push(int val){
            if(!queue1.isEmpty()) {
                queue1.offer(val);
            }else if(!queue2.isEmpty()) {
                queue2.offer(val);
            }else {
                queue1.offer(val);
            }
        }

        public int pop(){
            if(empty()){
                return -1;
            }
            if(!queue1.isEmpty()){
                int size = queue1.size();
                while(size != 1){
                    queue2.offer(queue1.poll());
                    size--;
                }
                return queue1.poll();
            }else {
                int size = queue2.size();
                while(size != 1){
                    queue1.offer(queue2.poll());
                    size--;
                }
                return queue2.poll();
            }
        }

        public int peek(){
            if(empty()){
                return -1;
            }
            if(!queue1.isEmpty()){
                int size = queue1.size();
                int val = 0;
                while(size != 0){
                    val = queue1.poll();
                    queue2.offer(val);
                    size--;
                }
                return val;
            }else {
                int size = queue2.size();
                int val = 0;
                while(size != 0){
                    val = queue2.poll();
                    queue1.offer(val);
                    size--;
                }
                return val;
            }
        }

        public boolean empty(){
            return queue1.isEmpty() && queue2.isEmpty();
        }
}
